package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Description:
 * @Create by: wanglei
 * @Date: 2021/11/7 12:55
 */
//试题选项
@DubboService
public class OptionApiImpl implements OptionApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //保存
    @Override
    public void save(Option option) {
        mongoTemplate.save(option);
    }

    //根据questionid查询
    @Override
    public List<Option> findByQuestionId(String questionId) {
        Query query = Query.query(Criteria.where("questionId").is(questionId));
        List<Option> options = mongoTemplate.find(query, Option.class);
        return options;
    }

    //根据id，查找
    @Override
    public Option findById(ObjectId optionId) {
        Option option = mongoTemplate.findById(optionId, Option.class);
        return option;
    }
}
